package com.example.controller;


import com.aliyun.oss.model.PutObjectResult;
import com.aliyuncs.exceptions.ClientException;
import com.common.result.Result;
import com.example.service.CommonService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping("/admin/common")
@Slf4j
public class CommonController {

    @Autowired
    private CommonService commonService;

    /**
     * 文件上传到阿里云oss
     * **/
    @PostMapping("/uploadOss")
    public Result upload(MultipartFile file) throws IOException, ClientException {

        PutObjectResult upload = commonService.upload(file);

        return Result.success(upload);
    }
    /**
     * 文件上传到本地
     * **/
    @PostMapping("/uploadLocal")
    public Result uploadLocal(MultipartFile file) throws IOException {
        String path = commonService.uploadLocal(file);
        return Result.success(path);
    }
}
